package com.bysj.money_diary;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener{
	private Button find_passward,register_submit;
	private EditText login_username = null,login_pho = null,login_password = null,login_password1 = null,login_ans = null;
	private CheckBox checkBox = null;
	private ImageView login_pic;
	private Data dataHelper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
		

	}

	private void initView() {
		register_submit = (Button) findViewById(R.id.register_submit);
		find_passward = (Button) findViewById(R.id.findpassward);
		checkBox = (CheckBox) findViewById(R.id.checkBox);
		login_username = (EditText) findViewById(R.id.login_username);
		login_password = (EditText) findViewById(R.id.login_password);
		login_password1 = (EditText) findViewById(R.id.login_password1);
		login_pho = (EditText) findViewById(R.id.login_pho);
		login_ans = (EditText) findViewById(R.id.login_ans);
		login_pic = (ImageView) findViewById(R.id.login_pic);
		
		register_submit.setOnClickListener(this); 
		find_passward.setOnClickListener(this); 
		login_pic.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		 	case R.id.findpassward:
		 		Intent intent = new Intent();
				intent.setClass(this, FindPasswordActivity.class); 
				startActivity(intent);
				break;
			case R.id.register_submit:                      
				register_check();  
				break;
			case R.id.login_pic:
				Intent intent1 = new Intent();
				intent1.setClass(this, RegisterShualianActivity.class); 
				startActivity(intent1);
				break;
			default:
				break;
		}
		
	}

	private void register_check() {
		if(RegisterShualianActivity.flag){
			if(isUserNameAndPwdValid()){//		
				dataHelper = new Data(this);//实例化
				User user = new User();//实例化
				String login_username_String = login_username.getText().toString().trim();
				String login_password_String = login_password.getText().toString().trim();
				String login_password1_String = login_password1.getText().toString().trim();
				String login_pho_String = login_pho.getText().toString().trim();
				String login_ans_String = login_ans.getText().toString().trim();
				
				if(dataHelper.haveUserInfo(login_username_String)){//用来判断账号是否有重复
					Toast.makeText(this, getString(R.string.name_already_exist, login_username_String),Toast.LENGTH_SHORT).show();  
		            return ;  
				}
				if(!isPho(login_pho_String)){//用来判断是否为合法手机号码
		        	Toast.makeText(this, getString(R.string.pho_form_fail, login_pho_String),Toast.LENGTH_SHORT).show();
		        	return;
		        }
				if(!isAns(login_ans_String)){
					Toast.makeText(this, getString(R.string.Ans_form_fail, login_ans_String),Toast.LENGTH_SHORT).show();
		        	return;
				}
				if(login_password_String.equals(login_password1_String)==false){     //两次密码输入不一样  
		            Toast.makeText(this, getString(R.string.pwd_not_the_same),Toast.LENGTH_SHORT).show();  
		            return ;  
		        } else {  
					user.setU_Name(login_username_String);
					user.setU_Password(login_password_String);
					user.setU_Pho(login_pho_String);
					user.setU_Beizhu(login_ans_String);
					user.setU_Pic(RegisterShualianActivity.picdata);
					dataHelper.SaveUserInfo(user);
		            if (!dataHelper.haveUserInfo(login_username_String)) { //通过账号查找，如果无此账号则查找失败 
		                Toast.makeText(this, getString(R.string.register_fail),Toast.LENGTH_SHORT).show();  
		            }else{  
		                Toast.makeText(this, getString(R.string.register_success),Toast.LENGTH_SHORT).show(); 
		                Intent intent_Register_to_Login = new Intent(RegisterActivity.this,LoginActivity.class) ;    //切换User Activity至Login Activity  
		                startActivity(intent_Register_to_Login);  
		                finish();  
		                dataHelper.CloseUser(user);
		            }  
		            
		        }
			}
		}else{
			Toast.makeText(this, getString(R.string.tip_shualian, RegisterShualianActivity.flag),Toast.LENGTH_SHORT).show();
			Intent intent1 = new Intent();
			intent1.setClass(RegisterActivity.this, RegisterShualianActivity.class);  
			startActivity(intent1);
		}
		
	}
	
	/*
	 * 判断输入是否为空
	 */
	private boolean isUserNameAndPwdValid() {
		if (login_username.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.account_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;  
        } else if (login_password.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.pwd_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;  
        }else if(login_password1.getText().toString().trim().equals("")) {  
            Toast.makeText(this, getString(R.string.pwd_check_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;  
        }else if(login_pho.getText().toString().trim().equals("")) {
        	Toast.makeText(this, getString(R.string.pho_check_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;
        }else if(login_ans.getText().toString().trim().equals("")){
        	Toast.makeText(this, getString(R.string.ans_check_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;
        }else if(!checkBox.isChecked()){
        	Toast.makeText(this, getString(R.string.checkBook_check_empty),  
                    Toast.LENGTH_SHORT).show();  
            return false;
        }
        return true;  
	}

	/*
	 * 用来判断回答是否符合要求是否合法
	 */
	private boolean isAns(String ans) {
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]{4}");
		Matcher m = p.matcher(ans);
	    return m.matches();	
	}

	/*
	 * 用来判断手机号是否合法
	 */
	private boolean isPho(String pho) {
		Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$");
		Matcher m = p.matcher(pho);
	    return m.matches();	
	}

	

	


	
}
